Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>import matplotlib.pyplot as plt
import numpy as np

def ab_distance(a,b):
    return np.sqrt(np.sum(np.square(a-b)))

def showc(data0,k0,center0,adismatrix0  ,  data1,k1,center1,adismatrix1):
    m,n = data0.shape
    mark0 = ['om', 'og', 'or']
    for i in range(m):
        markindex0 = int(adismatrix0[i, 0])
        plt.plot(data0[i, 0], data0[i, 1], mark0[markindex0])
    for i in range(k0):
        plt.plot(center0[i, 0], center0[i, 1], '*',markeredgecolor='black' , markersize=12)

    o,p = data1.shape
    mark1 = ['oy', 'oc', 'ob']
    for i in range(o):
        markindex1 = int(adismatrix1[i, 0])
        plt.plot(data1[i, 0], data1[i, 1], mark1[markindex1])
    for i in range(k1):
        plt.plot(center1[i, 0], center1[i, 1], '+',markeredgecolor='black' , markersize=12)
    plt.show()

def centers(data,k):
    n = np.shape(data)[1]
    center = np.mat(np.zeros((k,n)))
    for i in range(n):
        mini = np.min(data[:,i])
        rangei = np.max(data[:,i]) - mini
        center[:,i] = np.mat(np.ones((k,1))) + np.random.rand(k,1)*rangei
    return center

def k_means(data,k,disfunc=ab_distance):
    m = np.shape(data)[0]
    adismatrix = np.mat(np.zeros((m,2)))
    center = centers(data,k)
    clusterchange = True
    while clusterchange:
        clusterchange = False
        for i in range(m):
            dismin = np.inf
            indexmin = -1
            for j in range(k):
                distanceij = disfunc(center[j,:],data[i,:])
                if distanceij <= dismin:
                    dismin = distanceij
                    indexmin = j
            if adismatrix[i,0] != indexmin:
                clusterchange = True
                adismatrix[i,:] = indexmin, dismin**2
        for cent in range(k):
            ptsincluster = data[np.nonzero(adismatrix[:,0].A==cent)[0]]
            center[cent,:] = np.mean(ptsincluster,axis=0)
    return center,adismatrix


if __name__ == "__main__":

    data0 = []
    f0 = open("dataset_circles0.csv", 'r')
    for line in f0:
        data0.append([float(line.split(',')[0]), float(line.split(',')[1])])
    data0 = np.array(data0)
    k0 = 1
    center0 = centers(data0, k0)
    center0, adismatrix0 = k_means(data0, k0)

    data1 = []
    f1 = open("dataset_circles1.csv", 'r')
    for line in f1:
        data1.append([float(line.split(',')[0]), float(line.split(',')[1])])
    data1 = np.array(data1)
    k1 = 1
    center1 = centers(data1, k1)
    center1, adismatrix1 = k_means(data1, k1)

    showc(data0,k0,center0,adismatrix0  ,  data1,k1,center1,adismatrix1)
